

package com.huaxin.common.media;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;

import com.huaxin.common.BaseActivity;
import com.huaxin.common.R;
import com.huaxin.common.media.contract.SelectImageContract;

import java.io.Serializable;


/**
 * Created by huanghaibin_dev
 * on 2016/7/13.
 */
@SuppressWarnings("All")
public class SelectImageActivity extends BaseActivity implements SelectImageContract.Operator {
    private static final int    RC_CAMERA_PERM      = 0x03;
    private static final int    RC_EXTERNAL_STORAGE = 0x04;
    public static final  String KEY_CONFIG          = "config";

    private static volatile Callback mCallbackSnapshot;
    private                 Callback mCallback;
    private                 Config   mConfig;
    private Fragment fragment;

    public SelectImageActivity() {
        Callback callback = mCallbackSnapshot;
        if (callback == null) {
            throw new NullPointerException("SelectImageActivity's Callback isn't set null.");
        }
        mCallback = callback;
        mCallbackSnapshot = null;
    }

    public static void selectImage(Context context, boolean haveCamera, boolean needClip, Callback callBack) {
        if (callBack == null)
            throw new NullPointerException("SelectImageActivity's Callback isn't set null.");

        mCallbackSnapshot = callBack;

        Config config = new Config();
        config.selectCount = 1;
        config.haveCamera = haveCamera;
        config.selectedImages = null;
        config.needClip = needClip;
        config.needCompress = false;

        Intent intent = new Intent(context, SelectImageActivity.class);
        intent.putExtra(KEY_CONFIG, config);
        context.startActivity(intent);
    }

    public static void selectImages(Context context, int selectCount, boolean haveCamera, String[] selectedImages, Callback callBack) {
        if (callBack == null)
            throw new NullPointerException("SelectImageActivity's Callback isn't set null.");

        if (selectCount <= 0)
            throw new RuntimeException("SelectCount must >= 1");

        mCallbackSnapshot = callBack;

        // Set config
        Config config = new Config();
        config.selectCount = selectCount;
        config.haveCamera = haveCamera;
        config.selectedImages = selectedImages;
        config.needClip = false;
        config.needCompress = true;

        Intent intent = new Intent(context, SelectImageActivity.class);
        intent.putExtra(KEY_CONFIG, config);
        context.startActivity(intent);
    }


    protected void getBundleExtras(Bundle extras) {
        Serializable serializable = extras.getSerializable(KEY_CONFIG);
        if (serializable == null && serializable instanceof Config) {

        } else {
            mConfig = (Config) serializable;
            // We must need set one result
            //            mConfig.getSelectCount() >= 1;
        }
    }


    @Override
    protected int getContentViewId() {
        return R.layout.activity_select_image;
    }

    @Override
    public void initView() {
        super.initView();
        showSelect();
    }


    @Override
    public void showSelect() {
        try {
            fragment = Fragment.instantiate(this, SelectFragment.class.getName());
            getSupportFragmentManager()
                    .beginTransaction()
                    .replace(R.id.fl_content, fragment)
                    .addToBackStack(null)
                    .commitAllowingStateLoss();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void showClip(String path) {

        try {

            Fragment fragment = Fragment.instantiate(this, ClipFragment.class.getName());
            Bundle bundle = new Bundle();
            bundle.putString("path", path);
            fragment.setArguments(bundle);
            getSupportFragmentManager()
                    .beginTransaction()
                    .setCustomAnimations(R.anim.slide_in_from_right, R.anim.slide_out_to_left,
                            R.anim.slide_in_from_left, R.anim.slide_out_to_right)
                    .replace(R.id.fl_content, fragment)
                    .addToBackStack(null)
                    .commitAllowingStateLoss();
        } catch (Exception e) {
            e.printStackTrace();
        }

    }


    @Override
    public Callback getCallback() {
        return mCallback;
    }

    @Override
    public Config getConfig() {
        return mConfig;
    }


    @Override
    protected void onDestroy() {
        mConfig = null;
        mCallback = null;
        super.onDestroy();
    }

    @Override
    public void onBackPressed() {
        if (getSupportFragmentManager().getBackStackEntryCount() == 1) {
           if (fragment instanceof  SelectFragment){
               ((SelectFragment) fragment).onKeyBack();
           }
          //  finish();
        } else {
            super.onBackPressed();
        }
    }

    public static class Config implements Serializable {
        private int      selectCount;
        private boolean  haveCamera;
        private String[] selectedImages;
        private boolean  needClip;

        public boolean isNeedCompress() {
            return needCompress;
        }

        private boolean needCompress;

        public boolean isNeedClip() {
            return needClip;
        }

        public void setNeedClip(boolean needClip) {
            this.needClip = needClip;
        }


        public int getSelectCount() {
            return selectCount;
        }

        public void setSelectCount(int selectCount) {
            this.selectCount = selectCount;
        }

        public boolean isHaveCamera() {
            return haveCamera;
        }

        public void setHaveCamera(boolean haveCamera) {
            this.haveCamera = haveCamera;
        }

        public String[] getSelectedImages() {
            return selectedImages;
        }

        public void setSelectedImages(String[] selectedImages) {
            this.selectedImages = selectedImages;
        }
    }

    public static interface Callback {
        void doSelectDone(String[] images, String[] compress);
    }
}
